package com.laowang.project.system.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.laowang.common.utils.StringUtils;
import com.laowang.project.system.domain.SysDept;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.laowang.framework.aspectj.lang.annotation.Log;
import com.laowang.framework.aspectj.lang.enums.BusinessType;

import org.springframework.web.bind.annotation.*;
import com.laowang.project.system.domain.MyCarList;
import com.laowang.project.system.service.MyCarListService;
import com.laowang.framework.web.controller.BaseController;
import com.laowang.framework.web.domain.AjaxResult;
import com.laowang.common.utils.poi.ExcelUtil;
import com.laowang.framework.web.page.TableDataInfo;

/**
 * 车型Controller
 *
 * @author laowang
 * @date 2023-04-27
 */
@Api(tags = "车型接口")
@RestController
@RequestMapping("/carList")
public class MyCarListController extends BaseController {
    @Autowired
    private MyCarListService myCarListService;

    @ApiOperation(value = "小程序段 查询车型列表",notes = "")
    @GetMapping("/web/list")
    public AjaxResult weblist(MyCarList myCarList) {


        List<MyCarList> list = myCarListService.selectMyCarListList(null);
        List<MyCarList> myCarLists = buildDeptTree(list);
        return AjaxResult.success("操作成功",myCarLists);
    }

    public List<MyCarList> buildDeptTree(List<MyCarList> depts)
    {
        List<MyCarList> returnList = new ArrayList<>();
        /*List<MyCarList> tempList = new ArrayList<>();
        tempList.addAll(depts);*/
        for (Iterator<MyCarList> iterator = depts.iterator(); iterator.hasNext();) {
            MyCarList dept = (MyCarList) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            //System.out.println(dept);
            if (dept.getPid().equals(0)) {
                recursionFn(depts, dept);
                returnList.add(dept);
            }
        }
        //System.out.println("一级pid:"+returnList.size());
        return returnList;
    }

    /**
     * 递归列表
     * @param list
     * @param t
     */
    private void recursionFn(List<MyCarList> list, MyCarList t)
    {
        // 得到子节点列表
        List<MyCarList> childList = getChildList(list, t);
        t.setChildren(childList);
        for (MyCarList tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                // 判断是否有子节点
                Iterator<MyCarList> it = childList.iterator();
                while (it.hasNext())
                {
                    MyCarList n = (MyCarList) it.next();
                    recursionFn(list, n);
                }
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<MyCarList> getChildList(List<MyCarList> list, MyCarList t)
    {
        List<MyCarList> tlist = new ArrayList<>();
        Iterator<MyCarList> it = list.iterator();
        while (it.hasNext())
        {
            MyCarList n = (MyCarList) it.next();
            if (StringUtils.isNotNull(n.getPid()) && n.getPid().equals(t.getId()) )
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<MyCarList> list, MyCarList t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }

    /**
     * 查询车型列表
     */
    @ApiOperation(value = "查询车型列表",notes = "")
    @PreAuthorize("@ss.hasPermi('my:carList:list')")
    @GetMapping("/list")
    public TableDataInfo list(MyCarList myCarList) {
        startPage();
        List<MyCarList> list = myCarListService.selectMyCarListList(myCarList);
        return getDataTable(list);
    }

    /**
     * 导出车型列表
     */
    @ApiOperation(value = "导出车型列表",notes = "")
    @PreAuthorize("@ss.hasPermi('my:carList:export')")
    //@Log(title = "车型", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MyCarList myCarList) {
        List<MyCarList> list = myCarListService.selectMyCarListList(myCarList);
        ExcelUtil<MyCarList> util = new ExcelUtil<MyCarList>(MyCarList.class);
        return util.exportExcel(list, "carList");
    }

    /**
     * 获取车型详细信息
     */
    @ApiOperation(value = "获取车型详细信息",notes = "")
    @PreAuthorize("@ss.hasPermi('my:carList:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(myCarListService.selectMyCarListById(id));
    }

    /**
     * 新增车型
     */
    @ApiOperation(value = "新增车型",notes = "")
    @PreAuthorize("@ss.hasPermi('my:carList:add')")
    @Log(title = "车型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MyCarList myCarList) {
        return toAjax(myCarListService.insertMyCarList(myCarList));
    }

    /**
     * 修改车型
     */
    @ApiOperation(value = "修改车型",notes = "")
    @PreAuthorize("@ss.hasPermi('my:carList:edit')")
    @Log(title = "车型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MyCarList myCarList) {
        return toAjax(myCarListService.updateMyCarList(myCarList));
    }

    /**
     * 删除车型
     */
    @ApiOperation(value = "删除车型",notes = "")
    @PreAuthorize("@ss.hasPermi('my:carList:remove')")
    @Log(title = "车型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(myCarListService.deleteMyCarListByIds(ids));
    }
}
